package com.patterns.prototype.advMail;

import java.util.Random;

/**
 * Created by Xiao Yiyun on 2017/4/25.
 * 场景类
 */
public class Client {

    //发送账单的数量，这个值是从数据库获得
    private static int MAX_COUNT = 6;

    //发送邮件
    private static void sendMail(Mail mail) {
        System.out.println("标题" +
                            mail.getMailName() +
                            "\t收件人："+
                            mail.getReceiver() +
                            "\t...收件成功");
    }

    //获得指定长度的随机字符串
    private static String getRandString(int maxLength) {
        String source = "abcdefghigklmnopqrestuvwxyz";
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i=0; i<maxLength; i++) {
            sb.append(source.charAt(random.nextInt(source.length())));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        //模拟发送邮件
        int i = 0;
        //把模板定义出来，这个是从数据库获得
        Mail mail = new Mail(new AdvTemplate());
        mail.setTail("XX银行版权所有");
        while (i < MAX_COUNT) {
            //以下是每封邮件不同的地方
            Mail cloneMail = mail.clone();
            cloneMail.setApplication(getRandString(5) + "@" + getRandString(8) + ".com");
            //然后发送邮件
            sendMail(cloneMail);
            i ++;
        }
    }
}
